Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#File io' - 10 code snippet(s) found

 Sample 1. Method to get all files from a directory recursively

List<File> fileList = new ArrayList();

List<File> read(String dir) throws IOException{
File directory = new File(dir);
File[] fList = directory.listFiles();
for(File file:fList){
if(file.isDirectory()){
read(file.getPath());
} else {
fileList.add(file);
}
}
return fileList;
}

   Like      Feedback     file handling   file   isDirectory()  .getPath()   recursion   java.io.File


 Sample 2. Code Sample / Example / Snippet of org.apache.bcel.classfile.AnnotationEntry

    protected String dumpAnnotationEntries(final AnnotationEntry[] as)

{

final StringBuilder result = new StringBuilder();

result.append("[");

for (int i = 0; i < as.length; i++)

{

final AnnotationEntry annotation = as[i];

result.append(annotation.toShortString());

if (i + 1 < as.length) {

result.append(",");

}

}

result.append("]");

return result.toString();

}


   Like      Feedback      org.apache.bcel.classfile.AnnotationEntry


 Sample 3. getting file path, absolute path and canonical path

public static void main(String[] args){
String parent = null;
File file = new File("/file.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
try {
System.out.println(file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}

   Like      Feedback     file  absolute path  canonical path  canonical  file handling  java.io.IOException


 Sample 4. Code Sample / Example / Snippet of org.apache.bcel.classfile.AnnotationDefault

    public void testMethodAnnotations() throws ClassNotFoundException

{

final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotation");

final Method m = getMethod(clazz, "fruit");

final AnnotationDefault a = (AnnotationDefault) findAttribute(

"AnnotationDefault", m.getAttributes());

final SimpleElementValue val = (SimpleElementValue) a.getDefaultValue();

assertTrue("Should be STRING but is " + val.getElementValueType(), val

.getElementValueType() == ElementValue.STRING);

assertTrue("Should have default of bananas but default is "

+ val.getValueString(), val.getValueString().equals("bananas"));

}


   Like      Feedback      org.apache.bcel.classfile.AnnotationDefault


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 5. Code Sample / Example / Snippet of org.apache.bcel.classfile.Annotations

    public FieldGen(final Field field, final ConstantPoolGen cp) {

this(field.getAccessFlags(), Type.getType(field.getSignature()), field.getName(), cp);

final Attribute[] attrs = field.getAttributes();

for (final Attribute attr : attrs) {

if (attr instanceof ConstantValue) {

setValue(((ConstantValue) attr).getConstantValueIndex());

} else if (attr instanceof Annotations) {

final Annotations runtimeAnnotations = (Annotations)attr;

final AnnotationEntry[] annotationEntries = runtimeAnnotations.getAnnotationEntries();

for (final AnnotationEntry element : annotationEntries) {

addAnnotationEntry(new AnnotationEntryGen(element,cp,false));

}

} else {

addAttribute(attr);

}

}

}


   Like      Feedback      org.apache.bcel.classfile.Annotations


 Sample 6. Code Sample / Example / Snippet of org.apache.bcel.classfile.InnerClasses

public Attribute copy(final ConstantPool _constant_pool) {
final InnerClasses c = (InnerClasses) clone();
c.inner_classes = new InnerClass[inner_classes.length];
for (int i = 0; i < inner_classes.length; i++) {
c.inner_classes[i] = inner_classes[i].copy();
}
c.setConstantPool(_constant_pool);
return c;
}

   Like      Feedback      org.apache.bcel.classfile.InnerClasses  object initialization using clone


 Sample 7. Code Sample / Example / Snippet of org.apache.bcel.classfile.ParameterAnnotations

  public static ParameterAnnotationEntry[] createParameterAnnotationEntries(final Attribute[] attrs) {

final List<ParameterAnnotationEntry> accumulatedAnnotations = new ArrayList<>(attrs.length);

for (final Attribute attribute : attrs) {

if (attribute instanceof ParameterAnnotations) {

final ParameterAnnotations runtimeAnnotations = (ParameterAnnotations)attribute;

Collections.addAll(accumulatedAnnotations, runtimeAnnotations.getParameterAnnotationEntries());

}

}

return accumulatedAnnotations.toArray(new ParameterAnnotationEntry[accumulatedAnnotations.size()]);

}


   Like      Feedback      org.apache.bcel.classfile.ParameterAnnotations


 Sample 8. GunZip file using Apache Commons


   public void testGzipCreation() throws Exception {
final File input = getFile("test1.xml");
final File output = new File(dir, "test1.xml.gz");
final OutputStream out = new FileOutputStream(output);
try {
final CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream("gz", out);
try {
IOUtils.copy(new FileInputStream(input), cos);
} finally {
cos.close();
}
} finally {
out.close();
}
}

   Like      Feedback     Zip file using Apache Commons   Compress file using Apache Commons   GunZip file using Apache Commons  Code Sample / Example / Snippet of org.apache.commons.compress.compressors.CompressorOutputStream  IOUtils.copy


 Sample 9. Maven - SCM ( Source Code Management ) config in POM file

<scm>
<connection></connection>
<developerConnection></developerConnection>
<url></url>
</scm>

   Like      Feedback     maven  pom file  maven scm configuration


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 10. Rename a File

public static void main(String[] args) {
File oldFileName = new File("D:/oldFile.txt");
File newFileName = new File("D:/newFile.txt");
oldFileName.renameTo(newFileName);
}

   Like      Feedback     file io  input output  rename a file  renaming file



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner